package cn.com.yang.algorithm.v3;

import org.junit.Test;

import java.time.Clock;

public class SM4Test {
	private static final String SEED = "HEBANKABCDEF";
	 
    @Test
    public void test1() throws Exception {
        long time1 = Clock.systemUTC().millis() / 1000;
        String sourceFile = "E:\\test\\djbx\\file.docx";
        String targetFile = "E:\\test\\djbx\\file.encrypt";
        SM4Util.encryptFile(sourceFile, targetFile, SEED);
        long time2 = Clock.systemUTC().millis() / 1000;
        System.out.println("加密时间 = " + (time2 - time1));
    }
 
    @Test
    public void test2() throws Exception {
        long time1 = Clock.systemUTC().millis() / 1000;
        String targetFile = "E:\\test\\djbx\\file.encrypt";
        String sourceFile2 = "E:\\test\\djbx\\fileDecrypt.docx";
        SM4Util.decryptFile(targetFile, sourceFile2, SEED);
        long time2 = Clock.systemUTC().millis() / 1000;
        System.out.println("解密时间 = " + (time2 - time1));
    }
 
    @Test
    public void test3() throws Exception {
        String content = getLargeContent(1024000);
        System.out.println("content 长度 = " + content.length());
        long time1 = Clock.systemUTC().millis() / 1000;
        String en = SM4Util.sm4Encrypt(content, SEED);
//        System.out.println("en = " + en);
        long time2 = Clock.systemUTC().millis() / 1000;
        content = SM4Util.sm4Decrypt(en, SEED);
//        System.out.println("content = " + content);
        long time3 = Clock.systemUTC().millis() / 1000;
        System.out.println("加密时间 = " + (time2 - time1));
        System.out.println("解密时间 = " + (time3 - time2));
    }
 
    @Test
    public void test4() throws Exception {
        //String content = getLargeContent(1024000);
        String content = "[{OrgId=0102, AddrDtl=fdfdf, Phone=fdfd, TotalPrice=806.6, OrgName=eeeeeee, Buyer=eee, RcvAddr=[Ljava.lang.String;@77c96a80, "
        		+ "		OrderInfo=[{Price=12.5, ProdName=fdfd, Num=42, ProdId=P2022030900007240}, "
        		+ "{Price=4.4, ProdName=小宝贝ffffffffffffff, Num=64, ProdId=P2022030900007241},"
        		+ "{Price=12.5, ProdName=ggfgdgdfgdfgdfgdgdfgd43534, Num=42, ProdId=P2022030900007240},"
        		+ "{Price=12.5, ProdName=gdfgdfgdgdfgdfgdfg, Num=42, ProdId=P2022030900007240},"
        		+ "{Price=12.5, ProdName=ret3445345345345, Num=42, ProdId=P2022030900007240},"
        		+ "{Price=12.5, ProdName=gdfgdfgdfgdfgd, Num=42, ProdId=P2022030900007240},"
        		+ "{Price=12.5, ProdName=yrtyrtyrtyrtyrtyrtyrt, Num=42, ProdId=P2022030900007240},"
        		+ "{Price=12.5, ProdName=bgfnhgjjmkuoturtyrtyrt, Num=42, ProdId=P2022030900007240},"
                + "{Price=12.5, ProdName=dfsdfsdfwerewrwer, Num=42, ProdId=P2022030900007240}], OrderId=D20220614151243222850000000020}]";;
        System.out.println("content 长度 = " + content.length());
        //long time1 = Clock.systemUTC().millis() / 1000;
        long time1 = System.currentTimeMillis();
        String en = SM4Util.sm4EncryptLarge(content, SEED);
        System.out.println("加密后数据："+en);
        //long time2 = Clock.systemUTC().millis() / 1000;
        long time2 = System.currentTimeMillis();
        String de = SM4Util.sm4DecryptLarge(en, SEED);
        System.out.println("解密后数据："+de);
        //long time3 = Clock.systemUTC().millis() / 1000;
        long time3 = System.currentTimeMillis();
        System.out.println("加密时间 = " + (time2 - time1));
        System.out.println("解密时间 = " + (time3 - time2));
    }
 
    private static String getLargeContent(int count){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(getContent(i));
        }
        return sb.toString();
    }
 
    private static String getContent(int number){
        return "这是一段测试数据，结尾行号为"
                + number
                + "。";
    }
}
